package com.styytech.dadabao.ui.main;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.ui.map.MapFragment;
import com.styytech.dadabao.R;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.ui.my.MyFragment;
import com.styytech.dadabao.widge.pagersliding.PagerSlidingTabStrip;

public class FragmentMain extends Fragment {

//    private ProductFragment productFragment;
    private MyFragment personalFragment;
    private MapFragment mapFragment;
    /**
     * 顶部字体颜色
     */
    private String colorTopText = "#3488D9";
    /**
     * 顶部下划线颜色
     */
    private String colorTopLine = "#EA5514";

    /**
     * PagerSlidingTabStrip的实例
     */
    private PagerSlidingTabStrip tabs;

    /**
     * 获取当前屏幕的密度
     */
    private DisplayMetrics dm;


    @Override
    public void onCreate(Bundle savedInstanceState) {// 在前面执行
        super.onCreate(savedInstanceState);
        // 获取参数
        Bundle bundle = getArguments();
        if (null != bundle) {
            //
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, null);

        initView(view);

        return view;
    }

    private void initView(View view) {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }

    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor(colorTopText));// #45c01a
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor(colorTopLine));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    // FragmentPagerAdapter FragmentStatePagerAdapter //不能用FragmentPagerAdapter

    public class MyPagerAdapter extends FragmentStatePagerAdapter implements
            PagerSlidingTabStrip.IconTabProvider {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        //		private final String[] titles = { "产品", "个人", "消息", "推荐" };
        private final String[] titles = {"产品", "个人",  "地图"};

        private final int[] titlesIcon = {R.drawable.icon_home,
                R.drawable.icon_list, R.drawable.icon_mine};

        @Override
        public int getPageIconResId(int position) {
            // TODO Auto-generated method stub
            return titlesIcon[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position]; // 返回标题
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
//
//                    if (null == productFragment) {
//                        String newsUrl = ConstantsServerUrl.getNewsUrl();
//                        productFragment = ProductFragment.newInstance(newsUrl,
//                                ConstantsCommon.DATA_SRC_NEWS,
//                                ConstantsCommon.ADAPTER_TYPE_NEWS, null);
//                    }
//
//                    return productFragment;

                case 1:

                    if (null == personalFragment) {
                        personalFragment = new MyFragment();
                    }

                    return personalFragment;

                case 2:

                    if (null == mapFragment) {
                        mapFragment = new MapFragment();
                    }
                    return mapFragment;

                default:
                    return null;
            }
        }

    }
}
